package com.example.booksdeal.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.booksdeal.R;
import com.example.booksdeal.basic.BooksListActivity;

public class F2Fragment extends Fragment {

    private LinearLayout fenlei1,fenlei2,fenlei3,fenlei4,fenlei5,fenlei6,fenlei7,fenlei8;
    private TextView fenlei1_text1,fenlei2_text1,fenlei3_text1,fenlei4_text1,fenlei5_text1,fenlei6_text1,fenlei7_text1,fenlei8_text1;
    private OnClickListener listener;
    private String username;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.f2_fragment,container,false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getActivity().getIntent().getExtras();
        username = bundle.getString("username");
        initview(view);
    }

    private void initview(View view) {
        fenlei1 = view.findViewById(R.id.fenlei1);
        fenlei2 = view.findViewById(R.id.fenlei2);
        fenlei3 = view.findViewById(R.id.fenlei3);
        fenlei4 = view.findViewById(R.id.fenlei4);
        fenlei5 = view.findViewById(R.id.fenlei5);
        fenlei6 = view.findViewById(R.id.fenlei6);
        fenlei7 = view.findViewById(R.id.fenlei7);
        fenlei8 = view.findViewById(R.id.fenlei8);

        fenlei1_text1 = view.findViewById(R.id.fenlei1_text1);
        fenlei2_text1 = view.findViewById(R.id.fenlei2_text1);
        fenlei3_text1 = view.findViewById(R.id.fenlei3_text1);
        fenlei4_text1 = view.findViewById(R.id.fenlei4_text1);
        fenlei5_text1 = view.findViewById(R.id.fenlei5_text1);
        fenlei6_text1 = view.findViewById(R.id.fenlei6_text1);
        fenlei7_text1 = view.findViewById(R.id.fenlei7_text1);
        fenlei8_text1 = view.findViewById(R.id.fenlei8_text1);

        listener = new OnClickListener();

        fenlei1.setOnClickListener(listener);
        fenlei2.setOnClickListener(listener);
        fenlei3.setOnClickListener(listener);
        fenlei4.setOnClickListener(listener);
        fenlei5.setOnClickListener(listener);
        fenlei6.setOnClickListener(listener);
        fenlei7.setOnClickListener(listener);
        fenlei8.setOnClickListener(listener);
    }

    private class OnClickListener implements View.OnClickListener {
        Intent intent = new Intent(getActivity(), BooksListActivity.class);
        Bundle bundle = new Bundle();
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.fenlei1:
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                    bundle.putString("type",fenlei1_text1.getText().toString());
                    bundle.putString("username",username);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.fenlei2:
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                    bundle.putString("type",fenlei2_text1.getText().toString());
                    bundle.putString("username",username);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.fenlei3:
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                    bundle.putString("type",fenlei3_text1.getText().toString());
                    bundle.putString("username",username);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.fenlei4:
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                    bundle.putString("type",fenlei4_text1.getText().toString());
                    bundle.putString("username",username);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.fenlei5:
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                    bundle.putString("type",fenlei5_text1.getText().toString());
                    bundle.putString("username",username);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.fenlei6:
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                    bundle.putString("type",fenlei6_text1.getText().toString());
                    bundle.putString("username",username);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.fenlei7:
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                    bundle.putString("type",fenlei7_text1.getText().toString());
                    bundle.putString("username",username);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.fenlei8:
                    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  //只启动一个activity
                    bundle.putString("type",fenlei8_text1.getText().toString());
                    bundle.putString("username",username);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    }
}
